﻿using System.Runtime.InteropServices;

namespace Super.Hmi.Utils.Win32
{
    public class Win32
    {
        #region Win32 API 声明（用于获取屏幕信息）

        [DllImport("user32.dll")]
        private static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);

        [DllImport("user32.dll")]
        private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct MONITORINFO
        {
            public int cbSize;
            public RECT rcMonitor;
            public RECT rcWork;
            public uint dwFlags;
        }

        public const int MONITOR_DEFAULTTONEAREST = 0x00000002;

        // 外部使用
        public static IntPtr UserMonitorFromWindow(IntPtr hwnd, uint dwFlags) => MonitorFromWindow(hwnd, dwFlags);
        public static bool UserGetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi) => GetMonitorInfo(hMonitor, ref lpmi);

        #endregion

        #region Win32 API 锁屏
        [DllImport("user32.dll")]
        private static extern bool LockWorkStation();
        public static void LockScreen()
        {
            bool success = LockWorkStation();
            if (!success)
            {
                int errorCode = Marshal.GetLastWin32Error();
                throw new Exception($"锁屏失败，错误代码: {errorCode}");
            }
        }
        #endregion
    }
}
